<?php
/**
 * Created by PhpStorm.
 * User: wangx08
 * Date: 2015-04-15
 * Time: 16:49
 */

namespace common\actions;  

use common\error\ErrorCode;
use Guzzle\Http\Exception\HttpException;
use Yii;
use yii\base\Action;
use yii\base\Exception;
use yii\base\UserException;

class SystemError extends Action
{
    public $view;
    /**
     * @var string the name of the error when the exception name cannot be determined.
     * Defaults to "Error".
     */
    public $defaultName;
    /**
     * @var string the message to be displayed when the exception message contains sensitive information.
     * Defaults to "An internal server error occurred.".
     */
    public $defaultMessage;

    /**
     * Runs the action
     *
     * @return string result content
     */
    public function run()
    {

        if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
            return '';
        }

        if ($exception instanceof HttpException) {
            $code = $exception->statusCode;
        } else {
            $code = $exception->getCode();
        }
        if ($exception instanceof Exception) {
            $name = $exception->getName();
        } else {
            $name = $this->defaultName ?: Yii::t('yii', 'Error');
        }
        if ($code) {
            $name .= " (#$code)";
        }

        if ($exception instanceof UserException) {
            $message = $exception->getMessage();
        } else {
            $message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.');
        }

        if ($exception instanceof \yii\db\Exception) {
            $name = ErrorCode::ERR_KFSADMIN_DBERROR;
            $message = "数据库错误";
        }
        else{
            $name = ErrorCode::ERR_KFSADMIN_UNHANDLEDERROR;
        }
        return '{"retCode":"'.$name.'","errMsg":"'.$message.'"}';

    }
}
